<template>
  <div style="padding: 30px">
    <h2
      style="
        font-weight: 700;
        font-style: normal;
        font-size: 24px;
        color: #344563;
        margin-bottom: 20px;
      "
    >
      用户管理
    </h2>
    <el-row class="h">
      <el-button round id="userButton" size="mini" @click="fn"
        >添加用户</el-button
      >
    </el-row>
    <el-form :inline="true" :model="formInline" class="demo-form-inline">
      <el-form-item label="账号">
        <el-input v-model="formInline.user" placeholder="账号或姓名"></el-input>
      </el-form-item>
      <el-form-item label="角色">
        <el-select v-model="formInline.region" placeholder="器材库管">
          <el-option
            v-for="(v, i) in tableDat"
            :key="i"
            :label="v"
            :value="v"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="状态">
        <el-select v-model="formInline.state" placeholder="全部">
          <el-option label="启用" value="shanghai"></el-option>
          <el-option label="禁用" value="beijing"></el-option>
        </el-select>
      </el-form-item>
      <el-row>
        <el-button
          type="primary"
          icon="el-icon-zoom-in"
          size="mediu"
          @click="queryData"
          >搜索</el-button
        >
        <el-button
          type="success"
          icon="el-icon-plus"
          size="mediu"
          @click="getData"
          >重置</el-button
        >
      </el-row>
    </el-form>
    <el-table :data="tableData" style="width: 100%">
      <el-table-column prop="account" label="账号"> </el-table-column>
      <el-table-column prop="userName" label="姓名"> </el-table-column>
      <el-table-column prop="phoneNumber" label="手机号"> </el-table-column>
      <el-table-column prop="roleName" label="角色"> </el-table-column>
      <el-table-column prop="addTime" label="添加时间"> </el-table-column>
      <el-table-column prop="state" label="启用/禁用">
        <template template scope="scope">
          <el-switch
            v-model="scope.row.state"
            active-color="#13ce66"
            inactive-color="#ff4949"
          >
          </el-switch>
        </template>
      </el-table-column>

      <el-table-column prop="id" label="操作">
        <template template scope="scope">
          <!-- 编辑 -->
          <el-button
            @click="(dialog = true), dfa(scope.row)"
            type="primary"
            id="edit"
          >
            <i class="el-icon-edit"></i>
          </el-button>
          <el-drawer
            title="编辑用户"
            :before-close="handleClose"
            :visible.sync="dialog"
            direction="ltr"
            custom-class="demo-drawer"
            ref="drawer"
          >
            <div class="demo-drawer__content">
              <el-form :model="form">
                <el-form-item label="账号" :label-width="formLabelWidth">
                  <el-input
                    v-model="form.account"
                    autocomplete="off"
                  ></el-input>
                </el-form-item>
                <el-form-item label="姓名" :label-width="formLabelWidth">
                  <el-input v-model="form.name" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="手机号" :label-width="formLabelWidth">
                  <el-input
                    v-model="form.phoneNumber"
                    autocomplete="off"
                  ></el-input>
                </el-form-item>
                <el-form-item label="角色" :label-width="formLabelWidth">
                  <el-select
                    v-model="form.role"
                    placeholder="系统管理员"
                    autocomplete="off"
                  >
                    <el-option
                      v-for="(v, i) in tableDat"
                      :key="i"
                      :label="v"
                      :value="v"
                    ></el-option>
                  </el-select>

                  <!-- <el-input v-model="form.role" autocomplete="off"></el-input> -->
                </el-form-item>
              </el-form>
              <div class="demo-drawer__footer">
                <el-button @click="cancelForm">取 消</el-button>
                <el-button
                  type="primary"
                  @click="$refs.drawer.closeDrawer()"
                  :loading="loading"
                  >{{ loading ? "提交中 ..." : "确 定 " }}</el-button
                >
              </div>
            </div>
          </el-drawer>

          <el-button type="text" @click="open(scope.row.id)"
            ><i class="el-icon-delete"></i>
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <div class="block">
      <span class="demonstration">直接前往</span>
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page.sync="currentPage3"
        :page-size="limit"
        layout="prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </div>
  </div>
</template>
<style scoped>
#h {
  margin-bottom: 15px;
  font-weight: 200;
}
</style>
<style >
#yuan {
  background: #6554c0;
  color: black;
}
#edit {
  border: none;
  background: white;
  color: #2e4ae9;
}
</style>
</el-form>
<script>
export default {
  data() {
    return {
      formInline: {
        user: "",
        region: "",
        state: "",
      },
      form: {
        name: "",
        role: "",
        account: "",
        phoneNumber: "",
      },
      tableData: [],
      tableDat: [],
      currentPage3: 1,
      limit: 5,
      page: 1,
      total: 0,
      dialog: false,
      loading: false,
      formLabelWidth: "80px",
      timer: null,
      ida: " ",
      fgh: [],
    };
  },
  methods: {
    handleSizeChange: function (val) {
      console.log(`每页 ${val} 条`);
      this.id = val;
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
      this.page = val;
      this.getData();
    },
    fn: function () {
      this.$router.push("/home/tjyh");
    },
    df(sdf) {
      let sx = sdf;
      this.$alert("<div>frf</div>", "修改用户", {
        dangerouslyUseHTMLString: true,
      });
    },
    open(delld) {
      let del_id = delld;
      this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.$axios({
            url: "/user/delete",
            method: "delete",
            params: {
              id: del_id,
            },
          }).then((res) => {
            if (res.data.code == 0) {
              this.$message({
                type: "success",
                message: "删除成功!",
              });
              this.getData();
            } else {
              this.$message({
                type: "info",
                message: "删除失败！！！",
              });
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    dfa(row) {
      // console.log(row);
      this.ida = row.id;
      this.form.name = row.userName;
      this.form.role = row.roleName;
      this.form.account = row.account;
      this.form.phoneNumber = row.phoneNumber;
    },
    getData: function () {
      this.$axios({
        url: "/user/list",
        method: "post",
        params: {
          page: this.page,
          limit: 5,
        },
      }).then((res) => {
        if (res.data.code == 0) {
          for (var i = 0; i < res.data.data.length; i++) {
            this.fgh.push(res.data.data[i].state == 1 ? true : false);
          }
          //  console.log(this.fgh);
          this.tableData = res.data.data;
          for (var i = 0; i < this.tableData.length; i++) {
            this.tableData[i].state = this.fgh[i];
          }
          this.total = res.data.total;
        }
      });
    },
    postDat: function () {
      this.$axios({
        url: "/user/roleName",
        method: "post",
      }).then((res) => {
        if (res.data.code == 0) {
          // console.log(res.data.data)
          this.tableDat = res.data.data;
          // console.log(this.tableDat)
        }
      });
    },
    queryData: function () {
      this.$axios({
        url: "/user/list",
        method: "post",
        data: {
          account: this.formInline.user,
          roleName: this.formInline.region,
        },
        params: {
          page: this.page,
          limit: 5,
        },
      }).then((res) => {
        if (res.data.code == 0) {
          this.tableData = res.data.data;
          this.total = res.data.total;
        }
      });
    },
    handleClose(done) {
      if (this.loading) {
        return;
      }
      this.$confirm("确定要提交表单吗？")
        .then((_) => {
          this.$axios({
            url: "/user/update",
            method: "put",
            data: {
              id: this.ida,
              userName: this.form.name,
              roleName: this.form.role,
              account: this.form.account,
              phoneNumber: this.form.phoneNumber,
            },
          }).then((res) => {
            if (res.data.code == 0) {
              this.loading = true;
              this.timer = setTimeout(() => {
                done();
                // 动画关闭需要一定的时间
                setTimeout(() => {
                  this.loading = false;
                }, 400);
              }, 2000);
              this.getData();
            }
          });
        })
        .catch((_) => {});
    },
    cancelForm() {
      this.loading = false;
      this.dialog = false;
      clearTimeout(this.timer);
    },
  },
  mounted: function () {
    this.getData();
    this.postDat();
    this.queryData();
  },
};
</script>